package com.ja.openchat.views;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ja.openchat.R;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.App;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;

public class BottomSelectionDialog extends BottomPopupView{

    private final ChatInfo chat;
    private final OnItemClickListner listner;
    private UserAvatarView avatarView;
    private TextView headerName;
    private TextView tv_pinned;
    private TextView tv_notification;
    private TextView tv_delete;
    private String deleteLable = App.getInstanse().getString(R.string.delete_text);

    public BottomSelectionDialog(@NonNull Context context, ChatInfo chat,String deleteLable, OnItemClickListner listner) {
        super(context);
        this.chat = chat;
        this.listner = listner;
        this.deleteLable = deleteLable;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.msg_bottom_selection_view;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        avatarView = findViewById(R.id.user_image);
        headerName = findViewById(R.id.user_name);
        tv_pinned = findViewById(R.id.tv_pinned);
        tv_notification = findViewById(R.id.tv_notification);
        tv_delete = findViewById(R.id.tv_delete);

        if (chat != null) {
            if (chat.isGroup()) {//群组
                //群组名称
                headerName.setText(chat.title);
                //群组头像
                avatarView.showGroup(chat);
            } else {//单聊
                //对方昵称
                headerName.setText(UserInfo.userDisplayName(chat.userId()));
                //
                UserInfo user = TelegramManager.getTelegramManager().getTgData().contactInfo(chat.userId());
                if (user != null) {
                    avatarView.showUser(user);
                } else {
                    avatarView.showName(UserInfo.userDisplayName(chat.userId()));
                }
            }
            tv_pinned.setActivated(chat.is_pinned);
            tv_pinned.setText(chat.is_pinned?getContext().getString(R.string.cancle_top_text):getContext().getString(R.string.top_text));
            tv_notification.setActivated(chat.default_disable_notification);
            tv_notification.setText(chat.default_disable_notification?getContext().getString(R.string.cancel_do_not_disturb):getContext().getString(R.string.close_notifiy));
            if(!TextUtils.isEmpty(deleteLable)){
                tv_delete.setText(deleteLable);
            }
            tv_pinned.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listner != null) {
                        listner.clickPostion(0,chat);
                    }
                    dismiss();
                }
            });
            tv_notification.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listner != null) {
                        listner.clickPostion(1,chat);
                    }
                    dismiss();
                }
            });
            tv_delete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listner != null) {
                        listner.clickPostion(2,chat);
                    }
                    dismiss();
                }
            });
        }
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getAppHeight(getContext()) * 1f);
    }


    public interface OnItemClickListner {
        void clickPostion(int position,ChatInfo postion);
    }

}
